//app.js
App({
  onLaunch: function () {
    var that = this
    wx.getSetting({
      success: res => {
        // console.log(res)
        if (res.authSetting['scope.userInfo']) {
          that.globalData.isAuthorized =  true
          wx.getUserInfo({
            success: res => {
              // console.log(res)
              that.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (that.userInfoReadyCallback) {
                that.userInfoReadyCallback(res)
              }
            }
          })
        }else{
          that.globalData.isAuthorized =  false
        }
        if (res.authSetting['scope.werun'] && res.authSetting['scope.werun'] == true) {
          that.globalData.isWeRun =  true
        }else{
          that.globalData.isWeRun =  false
        }
      }
    })
    // 登录
    wx.login({
      success:function(login){
        // 发送请求
        wx.request({
            url: that.globalData.apiUrl + '/get_openid', //接口地址
            data: {
              code: login.code,
            },
            method: 'get',
          header: {
              'content-type': 'application/json' //默认值
          },
          success: function (res) {
            console.log(res)
            //判断用户是否存在
            if(res.data.code == '1'){
              //后台有数据
              that.globalData.userId = res.data.data.id;
              that.globalData.communityId = res.data.data.community_id
              // if(!that.globalData.isAuthorized){
              //   wx.navigateTo({
              //     url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ 'true',
              //   })
              // }
              if (that.userIdCallback) {
                that.userIdCallback(that.globalData.userId)
              }
              if(that.communityIdCallback){
                that.communityIdCallback(that.globalData.communityId)
              }
              if(that.globalData.userInfo){
                if(res.data.data.headimgurl != that.globalData.userInfo.avatarUrl || res.data.data.nickname != that.globalData.userInfo.nickName){
                  wx.request({
                    url: that.globalData.apiUrl + '/Api/GetUser/save_user1', //接口地址
                    data: {
                      id: res.data.data.id,
                      headimgurl: that.globalData.userInfo.avatarUrl,
                      nickname: that.globalData.userInfo.nickName
                    },
                    method: 'get',
                    header: {
                        'content-type': 'application/json' //默认值
                    },
                    success: function (saveRes) {
                      console.log(saveRes)
                    }
                  })
                }
              }
            }else if(res.data.code == '0'){
              that.globalData.openid = res.data.openid
              // if(that.globalData.isAuthorized){
              //   //用户没有登录过 但是已经授权
              //   wx.redirectTo({
              //     url: '/pages/login/login'
              //   })
              // }else{
              //   wx.navigateTo({
              //     url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ 'false',
              //   })
              // }
            }else{
              //报错  参数有错
            }
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null,
    apiUrl : 'https://jzj.freei.cc',
    isAuthorized: null,
    userId: null,
    city: null,
    communityId: 1,
    themeColor: '#ff7950',
    themeColor2: '#50d2c2',
    mainName: '健之家·康养智慧社区',
    identity: {},
    communityContent: {},
    pageNum: 20
  }
})
